<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="showSysCfg">添加配置</el-button>
        </el-col>
      </el-row>
      <el-table :data="sysCfgList" border stripe>
        <el-table-column label="标识" prop="flag"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="内容" prop="content"></el-table-column>
        <el-table-column label="添加时间" prop="addTime"></el-table-column>
        <el-table-column label="修改时间" prop="modifyTime"></el-table-column>
        <el-table-column label="修改">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.flag)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog title="添加配置" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="90px" class="demo-ruleForm">
        <el-form-item label="标识" prop="flag">
          <el-input v-model="addForm.flag" style="width: 80%;"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="addForm.description" style="width: 80%;"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="addForm.content"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSysCfg">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改配置" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="90px" class="demo-ruleForm">
        <el-form-item label="标识" prop="flag">
          <el-input v-model="editForm.flag"  :disabled="true" style="width: 80%;"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="editForm.description" style="width: 80%;"></el-input>
        </el-form-item>
         <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="editForm.content"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSysCfg">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Api from '@/api/Api.js'
import Util from '@/libs/utils'
export default {
  data() {
    return {
      queryInfo: {
      },
      sysCfgList: [],
      total: 0,
      addDialogVisible: false,
      addForm: {
        flag: '',
        description: '',
        content:''
      },
      //校验规则
      addFormRules: {
       
      },
      // 修改对话框的显示或隐藏
      editDialogVisible: false,
      editForm: {},
      editFormRules: {
        
      }
    }
  },
  created() {
    this.getSysCfgList()
  },
  methods: {
    showSysCfg() {
      this.addDialogVisible = true;
      this.getSysCfgList();
    },
    getSysCfgList() {
        Api.getSysCfgList(this.queryInfo).then(res => {
            if (res.success) {
                this.sysCfgList = res.data;
            } else {
                return this.$message.error('获取数据失败')
            }
        })
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 添加
    addSysCfg() {
      Api.saveSysCfg(this.addForm).then(res => {
        if (res.success) {
          this.addDialogVisible = false
          this.getSysCfgList()
          this.$message.success('添加成功')
        } else {
          return this.$message.error('添加失败: ' + res.msg)
        }
      })
    },
    async showEditDialog(flag) {
      Api.getSysCfg({'flag':flag}).then(res => {
            if (res.success) {
              this.editForm = res.data
              this.editDialogVisible = true
            } else {
              return this.$message.error('查询失败')
            }
        })
    },
    // 修改
    editSysCfg() {
      Api.saveSysCfg(this.editForm).then(res => {
        if (res.success) {
          this.editDialogVisible = false
          this.getSysCfgList()
          this.$message.success('修改成功！')
        } else {
          return this.$message.error('修改失败: ' + res.msg)
        }
      })
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    }
  },
}
</script>

<style lang="less" scoped>
</style>
